<template>
    <div>
        <el-table  style="width: 100%" border :data="records">
            <el-table-column type="index" label="序号" width="80" align="center">
            </el-table-column>

            <el-table-column prop="skuName" label="名称" >
            </el-table-column>

            <el-table-column prop="skuDesc" label="描述">
            </el-table-column>

            <el-table-column label="默认图片" width="110">
                <template slot-scope="{row,$index}">
                    <img :src="row.skuDefaultImg" alt="" style="width: 100px;height: 100px">
                </template>
            </el-table-column>

            <el-table-column prop="weight" label="重量(KG)" width="90">
            </el-table-column>

            <el-table-column prop="price" label="价格(元)" width="90">
            </el-table-column>

            <el-table-column prop="records" label="操作" width="300">
                <template slot-scope="{row,$index}">
                    <el-tooltip class="item" effect="dark" content="上架商品" placement="bottom-end" v-if="row.isSale==0">
                        <el-button type="success" icon="el-icon-top" size="mini" @click="onSale(row)"></el-button>
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="下架商品" placement="bottom-end" v-else>
                        <el-button type="warning" icon="el-icon-bottom" size="mini" @click="cancelSale(row)"></el-button>
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="修改商品" placement="bottom">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit"></el-button>
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="查看商品" placement="bottom">
                        <el-button type="info" icon="el-icon-info" size="mini" @click="getSkuInfo(row)"></el-button>
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="删除商品" placement="bottom-start">
                        <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <el-pagination
            style="text-align: center"
            :current-page="page"
            :page-size="limit"
            :page-sizes="[3,5,10]"
            layout=" prev, pager, next, jumper, ->, total , sizes"
            :total="total"
            @current-change="getSkuList"
            @size-change="handleSizeChange"
        >
        </el-pagination>
        <!-- 抽屉动画 -->
        <el-drawer :visible.sync="drawer" :direction="direction" :show-close="false" size="50%">
            <el-row>
                <el-col :span="5">名称</el-col>
                <el-col :span="16">{{ skuInfo.skuName }}</el-col>
            </el-row>

            <el-row>
                <el-col :span="5">描述</el-col>
                <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
            </el-row>

            <el-row>
                <el-col :span="5">价格</el-col>
                <el-col :span="16">{{ skuInfo.price }}元</el-col>
            </el-row>

            <el-row>
                <el-col :span="5">平台属性</el-col>
                <el-col :span="16" >
                    <el-tag type="success" v-for="(attr,index) in skuInfo.skuAttrValueList" :key="attr.id" style="margin-right: 10px">
                        {{attr.attrId}}--{{attr.valueId}}
                    </el-tag>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="5">商品图片</el-col>
                <el-col :span="16">
                    <el-carousel height="500px"  trigger="click">
                        <el-carousel-item v-for="(img,index) in skuInfo.skuImageList" :key="img.id" >
                            <img :src="img.imgUrl" alt="" style="width: 100%;">
                        </el-carousel-item>
                    </el-carousel>
                </el-col>
            </el-row>
        </el-drawer>
    </div>
</template>

<script>
import {logout} from "@/api/user";

export default {
    name: "Sku",
    data() {
        return {
            // 当前第几页
            page:1,
            // 每页展示的数量
            limit:10,
            // 保存Sku列表的数据
            records:[],
            // 存储数据总数
            total:0,
            // 控制抽屉动画
            drawer: false,
            // 抽屉动画效果，从右向左
            direction: 'rtl',
            // 存储服务器返回的Sku数据
            skuInfo:{},
        }
    },
    mounted() {
        // 页面加载完毕，获取Sku列表数据
        this.getSkuList()
    },
    methods:{
        async getSkuList(pages = 1) {
            this.page = pages
            let slResult = await this.$API.sku.reqSkuList(this.page,this.limit)
            // console.log(slResult)
            if (slResult.code == 200) {
                this.records = slResult.data.records
                this.total = slResult.data.total
            }
        },
        // 每页展示的数量发生时调用
        handleSizeChange(limit) {
            this.limit = limit
            this.getSkuList()
        },
        // 商品上架 ，点击变为 下架商品 按钮
        async onSale(row) {
            let onResult = await this.$API.sku.reqOnSale(row.id)
            // console.log(onResult)
            if (onResult.code == 200) {
                // 返回成功后，变为 下架商品 按钮
                row.isSale = 1          // isSale 为 0 时，展示 上架按钮，点击
                this.$message.success('商品上架成功！')
            }
        },
        // 商品下架 ，点击变为 上架商品 按钮
        async cancelSale(row) {
            let cancelResult = await this.$API.sku.reqCancelSale(row.id)
            // console.log(cancelResult)
            if (cancelResult.code == 200) {
                // 返回成功后，变为 上架商品 按钮
                row.isSale = 0          // isSale 为 0 时，展示 上架按钮，点击
                this.$message.warning('商品下架成功')
            }
        },
        // 修改商品
        edit() {
            this.$message.info('正在开发中......')
        },
        // 查看SKU详情
        async getSkuInfo(row) {
            // 点击查看 sku按钮，即弹出抽屉效果
            this.drawer = true
            // 获取SKU数据
            let qGSBIResult = await this.$API.sku.reqGetSkuById(row.id)
            // console.log(qGSBIResult)
            if (qGSBIResult.code == 200) {
                this.skuInfo = qGSBIResult.data
            }
        },
    },
}
</script>

<style >
.box {
    width: 400px;
}

.box .top {
    text-align: center;
}

.box .left {
    float: left;
    width: 60px;
}

.box .right {
    float: right;
    width: 60px;
}

.box .bottom {
    clear: both;
    text-align: center;
}

.box .item {
    margin: 4px;
}

.box .left .el-tooltip__popper,
.box .right .el-tooltip__popper {
    padding: 8px 10px;
}
.el-row .el-col-5 {
    font-size: 18px;
    text-align: right;
}
.el-col {
    margin: 10px 10px;
}
.el-carousel__item img {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
}

.el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
}
/* 修改下方小白条为小圆球*/
.el-carousel__button {
    width: 20px;
    height: 20px;
    background-color: purple;
    border-radius: 50%;
}
</style>
